********************************************************************************
** 	TITLE:		il2009_polls		                                          ** 	
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATE:		October 2022 					                              **	
**  VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Import data

import excel surveys, firstrow sheet(il2009) clear
destring _all, replace

* Generate survey date

gen date = date(polldate, "MDY")
format %tdMon_DD,_CCYY date
drop polldate
rename date polldate

* Generate election date

gen edate = 20090210

gen electiondate = date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

* Time of survey

gen time = (electiondate - polldate)

* Percentage of seats

replace kadima = (kadima/120)*100
replace labor = (labor/120)*100
replace shas = (shas/120)*100
replace likud = (likud/120)*100
replace beiteinu = (beiteinu/120)*100
replace ual = (ual/120)*100
replace hadash = (hadash/120)*100
replace balad = (balad/120)*100
replace home = (home/120)*100
replace union = (union/120)*100
replace gil = (gil/120)*100
replace utj = (utj/120)*100
replace meretz = (meretz/120)*100
replace greens = (greens/120)*100

* Generate mean vote intention value by day

bysort time : egen kadimavote = mean(kadima)
bysort time : egen laborvote = mean(labor)
bysort time : egen shasvote = mean(shas)
bysort time : egen likudvote = mean(likud)
bysort time : egen beiteinuvote = mean(beiteinu)
bysort time : egen ualvote = mean(ual)
bysort time : egen hadashvote = mean(hadash)
bysort time : egen baladvote = mean(balad)
bysort time : egen homevote = mean(home)
bysort time : egen unionvote = mean(union)
bysort time : egen gilvote = mean(gil)
bysort time : egen utjvote = mean(utj)
bysort time : egen meretzvote = mean(meretz)
bysort time : egen greensvote = mean(greens)

sort time

* Drop duplicates

duplicates tag polldate, gen(dup)
duplicates drop polldate, force
drop dup

* Reshape the dataset

rename kadima v_kadima
rename labor v_labor
rename shas v_shas
rename likud v_likud
rename beiteinu v_beiteinu
rename ual v_ual
rename hadash v_hadash
rename balad v_balad
rename home v_home
rename union v_union
rename gil v_gil
rename utj v_utj
rename meretz v_meretz
rename greens v_greens

reshape long v_, i(polldate) j(party) string

rename v_ vote

keep polldate electiondate party vote poll time

order poll polldate electiondate time party vote

* Generate rank of parties

gsort polldate -vote

bysort polldate : gen rank = _n

gen first = party if rank == 1
gen second = party if rank == 2
gen third = party if rank == 3

bysort polldate : gen winner = first[1]
bysort polldate : gen runnerup = second[2]
bysort polldate : gen thirdplace = third[3]

* Generate poll margin

bysort polldate : gen pollmar = vote[1] - vote[2]

* Drop duplicates

duplicates tag polldate, gen(dup)
duplicates drop polldate, force
drop dup

* Misleading poll

gen misleading = 0 if winner == "kadima" & pollmar >=1
replace misleading = 1 if misleading!=0

* Save

drop if time == 0 | time == .

keep polldate pollmar misleading time

save "il2009_polls.dta", replace